package com.wg.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wg.common.core.domain.PageQuery;
import com.wg.common.core.domain.TableDataInfo;
import com.wg.common.exception.ServiceException;
import com.wg.common.utils.StringUtils;
import com.wg.file.entity.UploadResult;
import com.wg.file.factory.FileFactory;
import com.wg.file.service.IFileStrategy;
import com.wg.system.entity.SysFile;
import com.wg.system.mapper.SysFileMapper;
import com.wg.system.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * OSS对象存储表(SysFile)服务实现类
 *
 * @author wg
 * @date 2022-05-06 11:18:25
 */
@Slf4j
@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {

    @Override
    public TableDataInfo<SysFile> queryPageList(SysFile file, PageQuery pageQuery) {
        Map<String, Object> params = file.getParams();
        LambdaQueryWrapper<SysFile> lqw = new LambdaQueryWrapper<SysFile>()
                .like(StringUtils.isNotBlank(file.getFileName()), SysFile::getFileName, file.getFileName())
                .like(StringUtils.isNotBlank(file.getOriginalName()), SysFile::getOriginalName, file.getOriginalName())
                .eq(StringUtils.isNotBlank(file.getFileSuffix()), SysFile::getFileSuffix, file.getFileSuffix())
                .eq(StringUtils.isNotBlank(file.getUrl()), SysFile::getUrl, file.getUrl())
                .between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
                        SysFile::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"))
                .eq(StringUtils.isNotBlank(file.getCreateBy()), SysFile::getCreateBy, file.getCreateBy())
                .eq(StringUtils.isNotBlank(file.getService()), SysFile::getService, file.getService());
        Page<SysFile> page = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public SysFile upload(MultipartFile file) {
        String originalFileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalFileName, originalFileName.lastIndexOf("."), originalFileName.length());
        IFileStrategy storage = FileFactory.instance();
        UploadResult uploadResult;
        try {
            uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType());
        } catch (IOException e) {
            throw new ServiceException(e.getMessage());
        }
        // 保存文件信息
        SysFile sysFile = new SysFile();
        sysFile.setUrl(uploadResult.getUrl());
        sysFile.setFileSuffix(suffix);
        sysFile.setFileName(uploadResult.getFilename());
        sysFile.setOriginalName(originalFileName);
        sysFile.setService(storage.getServiceType().getValue());
        baseMapper.insert(sysFile);
        return sysFile;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, boolean isValid) {
        if (isValid) {
            // 做一些业务上的校验,判断是否需要校验
        }
        List<SysFile> list = baseMapper.selectBatchIds(ids);
        for (SysFile sysFile : list) {
            IFileStrategy storage = FileFactory.instance(sysFile.getService());
            storage.delete(sysFile.getUrl());
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}

